/*   1:    */ package nl.siegmann.epublib.chm;
/*   2:    */ 
/*   3:    */ import java.io.IOException;
/*   4:    */ import java.io.InputStream;
/*   5:    */ import java.util.ArrayList;
/*   6:    */ import java.util.List;
/*   7:    */ import javax.xml.parsers.ParserConfigurationException;
/*   8:    */ import javax.xml.xpath.XPath;
/*   9:    */ import javax.xml.xpath.XPathConstants;
/*  10:    */ import javax.xml.xpath.XPathExpressionException;
/*  11:    */ import javax.xml.xpath.XPathFactory;
/*  12:    */ import nl.siegmann.epublib.domain.Resource;
/*  13:    */ import nl.siegmann.epublib.domain.Resources;
/*  14:    */ import nl.siegmann.epublib.domain.TOCReference;
/*  15:    */ import nl.siegmann.epublib.util.ResourceUtil;
/*  16:    */ import org.apache.commons.lang.StringUtils;
/*  17:    */ import org.htmlcleaner.CleanerProperties;
/*  18:    */ import org.htmlcleaner.DomSerializer;
/*  19:    */ import org.htmlcleaner.HtmlCleaner;
/*  20:    */ import org.htmlcleaner.TagNode;
/*  21:    */ import org.w3c.dom.Document;
/*  22:    */ import org.w3c.dom.Element;
/*  23:    */ import org.w3c.dom.Node;
/*  24:    */ import org.w3c.dom.NodeList;
/*  25:    */ 
/*  26:    */ public class HHCParser
/*  27:    */ {
/*  28:    */   public static final String DEFAULT_HTML_INPUT_ENCODING = "Windows-1251";
/*  29:    */   
/*  30:    */   public static List<TOCReference> parseHhc(InputStream hhcFile, Resources resources)
/*  31:    */     throws IOException, ParserConfigurationException, XPathExpressionException
/*  32:    */   {
/*  33: 40 */     HtmlCleaner htmlCleaner = new HtmlCleaner();
/*  34: 41 */     CleanerProperties props = htmlCleaner.getProperties();
/*  35: 42 */     TagNode node = htmlCleaner.clean(hhcFile);
/*  36: 43 */     Document hhcDocument = new DomSerializer(props).createDOM(node);
/*  37: 44 */     XPath xpath = XPathFactory.newInstance().newXPath();
/*  38: 45 */     Node ulNode = (Node)xpath.evaluate("body/ul", hhcDocument.getDocumentElement(), XPathConstants.NODE);
/*  39:    */     
/*  40: 47 */     List<TOCReference> sections = processUlNode(ulNode, resources);
/*  41: 48 */     return sections;
/*  42:    */   }
/*  43:    */   
/*  44:    */   private static List<TOCReference> processUlNode(Node ulNode, Resources resources)
/*  45:    */   {
/*  46: 65 */     List<TOCReference> result = new ArrayList();
/*  47: 66 */     NodeList children = ulNode.getChildNodes();
/*  48: 67 */     for (int i = 0; i < children.getLength(); i++)
/*  49:    */     {
/*  50: 68 */       Node node = children.item(i);
/*  51: 69 */       if (node.getNodeName().equals("li"))
/*  52:    */       {
/*  53: 70 */         List<TOCReference> section = processLiNode(node, resources);
/*  54: 71 */         result.addAll(section);
/*  55:    */       }
/*  56: 72 */       else if (node.getNodeName().equals("ul"))
/*  57:    */       {
/*  58: 73 */         List<TOCReference> childTOCReferences = processUlNode(node, resources);
/*  59: 74 */         if (result.isEmpty()) {
/*  60: 75 */           result = childTOCReferences;
/*  61:    */         } else {
/*  62: 77 */           ((TOCReference)result.get(result.size() - 1)).getChildren().addAll(childTOCReferences);
/*  63:    */         }
/*  64:    */       }
/*  65:    */     }
/*  66: 81 */     return result;
/*  67:    */   }
/*  68:    */   
/*  69:    */   private static List<TOCReference> processLiNode(Node liNode, Resources resources)
/*  70:    */   {
/*  71: 86 */     List<TOCReference> result = new ArrayList();
/*  72: 87 */     NodeList children = liNode.getChildNodes();
/*  73: 88 */     for (int i = 0; i < children.getLength(); i++)
/*  74:    */     {
/*  75: 89 */       Node node = children.item(i);
/*  76: 90 */       if (node.getNodeName().equals("object"))
/*  77:    */       {
/*  78: 91 */         TOCReference section = processObjectNode(node, resources);
/*  79: 92 */         if (section != null) {
/*  80: 93 */           result.add(section);
/*  81:    */         }
/*  82:    */       }
/*  83: 95 */       else if (node.getNodeName().equals("ul"))
/*  84:    */       {
/*  85: 96 */         List<TOCReference> childTOCReferences = processUlNode(node, resources);
/*  86: 97 */         if (result.isEmpty()) {
/*  87: 98 */           result = childTOCReferences;
/*  88:    */         } else {
/*  89:100 */           ((TOCReference)result.get(result.size() - 1)).getChildren().addAll(childTOCReferences);
/*  90:    */         }
/*  91:    */       }
/*  92:    */     }
/*  93:104 */     return result;
/*  94:    */   }
/*  95:    */   
/*  96:    */   private static TOCReference processObjectNode(Node objectNode, Resources resources)
/*  97:    */   {
/*  98:123 */     TOCReference result = null;
/*  99:124 */     NodeList children = objectNode.getChildNodes();
/* 100:125 */     String name = null;
/* 101:126 */     String href = null;
/* 102:127 */     for (int i = 0; i < children.getLength(); i++)
/* 103:    */     {
/* 104:128 */       Node node = children.item(i);
/* 105:129 */       if (node.getNodeName().equals("param"))
/* 106:    */       {
/* 107:130 */         String paramName = ((Element)node).getAttribute("name");
/* 108:131 */         if ("Name".equals(paramName)) {
/* 109:132 */           name = ((Element)node).getAttribute("value");
/* 110:133 */         } else if ("Local".equals(paramName)) {
/* 111:134 */           href = ((Element)node).getAttribute("value");
/* 112:    */         }
/* 113:    */       }
/* 114:    */     }
/* 115:138 */     if ((!StringUtils.isBlank(href)) && (href.startsWith("http://"))) {
/* 116:139 */       return result;
/* 117:    */     }
/* 118:141 */     if (!StringUtils.isBlank(name))
/* 119:    */     {
/* 120:142 */       Resource resource = resources.getByHref(href);
/* 121:143 */       if (resource == null)
/* 122:    */       {
/* 123:144 */         resource = ResourceUtil.createResource(name, href);
/* 124:145 */         resources.add(resource);
/* 125:    */       }
/* 126:147 */       result = new TOCReference(name, resource);
/* 127:    */     }
/* 128:149 */     return result;
/* 129:    */   }
/* 130:    */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.chm.HHCParser
 * JD-Core Version:    0.7.0.1
 */